package dfs;

import entity.tree.TreeNode;

public class leetcode814二叉树剪枝 {
    public TreeNode pruneTree(TreeNode root) {
        TreeNode pre = new TreeNode(-1);
        pre.right = root;
        dfs(root, pre, 1);
        return pre.right;
    }

    public void dfs(TreeNode root, TreeNode pre, int type) {
        if (root == null) {
            return;
        }
        dfs(root.left, root, 0);
        dfs(root.right, root, 1);
        if (root.left == null && root.right == null && root.val == 0) {
            if (type == 0) {
                pre.left = null;
            } else {
                pre.right = null;
            }
        }
    }
}
